<template>
    <div class="back_contain_n">	
        <!-- 头部搜索 -->
        <div class="Search_Top_Part">
            <div class="search_list" >
                <div>				
                    <div class="input_flex">
                        <el-select clearable v-model="queryForm.processName" placeholder="工序" @change="processNameChange">
                            <el-option v-for="(item,index) in processNameList" :key="index" :label="item" :value="item"></el-option>
                        </el-select>
                    </div>			
                    <div class="input_flex">
                        <el-select clearable v-model="queryForm.processCategory" placeholder="工段">
                            <el-option v-for="(item,index) in processCategoryList" :key="index" :label="item" :value="item"></el-option>
                        </el-select>
                    </div>
                    <div class="input_flex">
                        <el-input clearable v-model="queryForm.deviceCode" placeholder="设备号"></el-input>
                    </div>
                    <div class="input_flex">
                        <el-input clearable v-model="queryForm.deviceName" placeholder="设备名称"></el-input>
                    </div>
                    <div class="input_flex">
                        <el-select clearable v-model="queryForm.status" placeholder="状态">
							<el-option label="启用" value="0"></el-option>
							<el-option label="停用" value="1"></el-option>
                        </el-select>
                    </div>
                </div>
            </div>
            <div class="search_bt">
                <span class="zll-search" @click="getList">搜索</span>
                <span class="zll-search-reset" @click="searchReset()">重置</span>
            </div>
        </div>
		<div class="Search_Top_Input">
			<div class="addNew">
				<span @click="edit({},'add')" v-if="hasRole('') ">设备注册</span>
			</div>
		</div>
        <!-- table -->
        <sys-table  
            :tableData="tableData" 
            :tableLoading="tableLoading" 
            :tableHeader="tableHeader"
            :scopeWidth="120"
			ref="systable"
        >
            <template slot-scope="scope" slot="operate">
                <el-button @click="edit(scope.row,'see')" type="text" size="small">查看</el-button>
                <el-button @click="edit(scope.row,'edit')" type="text" size="small">编辑</el-button>
				<el-button @click="deleteRow(scope.row)" type="text" size="small">删除</el-button>
            </template>
        </sys-table>
        <div class="zll-dialog">
            <popout :title="'设备 · ' + (type == 'edit'?'编辑':(type =='see'?'查看':'注册'))" :visible.sync="addDialog" v-if="addDialog" class="largeSize">
                <add ref="add" slot="content" :editData="editData" :type="type" :processNames="processNameList" :processMap1="processMap" @refreshData="refreshData"></add>
                <template slot="bottom">
                    <p class="zll-botton Info" @click="addDialog = false">关 闭</p>
                    <p class="zll-botton" v-if="type !== 'see'" @click="()=>{this.$refs.add.submitForm('addForm')}">提交</p>
                </template>
            </popout>
        </div> 
    </div>
</template>

<script>
import Add from './add'
export default {
    data(){
        return {
            tableLoading:true, //table刷新
            type: '',
            editData: {},
			queryForm: {
				processName: '',
				processCategory: '',
				deviceCode: '',
				deviceName: '',
				status: '',
            },
            tableData: [
                {}
            ],
            tableHeader:[],
            addDialog: false,

			processNameList: ["配拌投料","挤塑直切","成型","包装","电表"],
			processMap: {
            	"配拌投料":["U直","伸直","普直","通用"],
            	"挤塑直切":["U直","伸直","普直"],
            	"成型":["成型"],
            	"包装":["U型","伸缩","单支连排","多支"],
            	"电表":["电表"]
			},
			processCategoryList: []
        }
    },
    methods: {
        getList(){//获取表格数据
        	let self = this;
            self.tableLoading = true;
			self.$refs.systable.resetPage();
			self.$http({
				url: "/jinl/device/getDeviceSelect",
				method: "post",
				data: JSON.stringify(self.queryForm),
				dataType: 'json',
				contentType: 'application/json',
			}).then(resp=>{
				if (resp.success){
					self.tableHeader =  [
						{"columnValue": "processName", "columnName": "工序名称" },
						{"columnValue": "processCategory", "columnName": "工段" },
						{"columnValue": "deviceCode", "columnName": "设备号" },
						{"columnValue": "deviceName", "columnName": "设备名称" },
						{"columnValue": "deviceStatusCn", "columnName": "状态" ,refColor:true},
						{"columnValue": "createrName", "columnName": "创建人" },
					];

					for (let data of resp.result){
						data.createrName = data.createrName+"  "+data.createTimeFt;
						data.editorName = data.editorName+"  "+data.updateTimeFt;
					}
					self.tableData = resp.result;
					self.tableLoading = false;
				}
			})
        },

        edit(val,type) {
        	let self = this;
			self.addDialog = true;
			self.$nextTick(()=> {
				self.type = type;
				self.editData = Object.assign({},val);
			});
        },

		refreshData() {
			this.getList();
			this.addDialog = false;

		},

        searchReset() { //重置搜索
            this.queryForm = {
				processName: '',processCategory: '',deviceCode: '',deviceName: '',status: ''
            };
            this.getList();
        },

		processNameChange(val) {
        	let self = this;
        	self.queryForm.processCategory = '';
        	self.processCategoryList = self.processMap[val];
		},

		deleteRow(rowData) {
			let self = this;

			self.$confirm('是否确认删除？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				self.$http({
					url: '/jinl/device/deleteDevice',
					method: "post",
					data: JSON.stringify({ids: [rowData.id]}),
					dataType: 'json',
					contentType: 'application/json',
				}).then(resp => {
					if (resp.success) {
						self.getList();
						self.$message.success('删除成功！');
						return
					}
					self.$message.error(resp.message);
				});
			});
		},
    },
    mounted(){
        let self = this;
        self.getList()
        self.$eventBus.$on("updateWorkList",function () {
            self.addDialog = false;
            self.getList()
        })
    },
    components: {
        Add
    },
}
</script>

<style lang="scss" scoped>
@import "@/style/SearchTop.scss";
.leftList {
    display: flex;
    margin-bottom: 15px;
    li {
        list-style: none;
        width: 100px;
        height: 30px;
        line-height: 28px;
        border-bottom: 1px solid #fff;
        color: #333;
        text-align: center;
        cursor: pointer;
        margin-bottom: 10px;
        transition: all .5s;
        position: relative;
        display: flex;
        justify-content: center;
        &.active {
            color: #ff9935;
            border-color: #ff9935;
            position: relative;
            font-weight: 600;
            font-size: 14px;
        }
    }
}
</style>